package com.burgeon.backend.EnterpriseWeChat.rest.Analysis;

import com.burgeon.backend.EnterpriseWeChat.entity.StoreKpi;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 购物车推荐
 */
public class getStoreKpiPrice extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getStoreKpiPrice(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<StoreKpi>  getData(String storeId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(storeId), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<StoreKpi> storeKpis= new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                StoreKpi storeKpi = new StoreKpi();
//                storeKpi.setRetailCount(jsonArray.getJSONArray(i).getString(0));
//                storeKpi.setLDL(jsonArray.getJSONArray(i).getString(1));
//                storeKpi.setJDJ(jsonArray.getJSONArray(i).getString(2));
//                storeKpi.setKDJ(jsonArray.getJSONArray(i).getString(2));
//                storeKpi.setAvgDiscount(jsonArray.getJSONArray(i).getString(2));
                storeKpi.setBillDate(jsonArray.getJSONArray(i).getString(2));
                storeKpis.add(storeKpi);
            }
        }
        return storeKpis;
    }

    private MultiValueMap Data(String storeId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

//        colums.add("TOT_COUNT_RETAIL");//成交人数
//        colums.add("LDL");//连带率
        colums.add("JDJ");//平均商品价
        colums.add("KDJ");//客单价
//        colums.add("AVG_DISCOUNT");//折扣率
        colums.add("BILLDATE");//日期

        params.put("columns", colums);
        params.put("table", "T_SALE_SINGLE01");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();

        paramss.put("column","'C_STORE_ID'");
        String a = "'"+storeId+"'";
        paramss.put("condition",a);

        HashMap orderby = new HashMap();

        orderby.put("column", "BILLDATE");
        orderby.put("asc", "true");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getStoreKpiPrice g = new getStoreKpiPrice("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("3128"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
